<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item
        label="订单编号"
        prop="orderNumber"
      >
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item
        label="教师姓名"
        prop="tutorName"
      >
        <el-input
          v-model="queryParams.tutorName"
          placeholder="请输入教师姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="教师电话"
        prop="tutorPhone"
      >
        <el-input
          v-model="queryParams.tutorPhone"
          placeholder="请输入教师电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item
        label="学生姓名"
        prop="userName"
      >
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="学生电话"
        prop="userPhone"
      >
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入学生联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="订单金额"
        prop="totalFee"
      >
        <el-input
          v-model="queryParams.totalFee"
          placeholder="请输入订单总金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="开始日期"
        prop="startDate"
      >
        <el-date-picker
          clearable
          v-model="queryParams.startDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="结束日期"
        prop="endDate"
      >
        <el-date-picker
          clearable
          v-model="queryParams.endDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="创建时间"
        prop="createdAt"
      >
        <el-date-picker
          clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
    </el-form>

    <el-row
      :gutter="10"
      class="mb8"
    >
      <el-col :span="1.5"> </el-col>
      <el-col :span="1.5"> </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:orders:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="ordersList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      />

      <el-table-column
        label="订单编号"
        align="center"
        prop="orderNumber"
      />

      <el-table-column
        label="教师姓名"
        align="center"
        prop="tutorName"
      />
      <el-table-column
        label="教师电话"
        align="center"
        prop="tutorPhone"
      />

      <el-table-column
        label="学生姓名"
        align="center"
        prop="userName"
      />
      <el-table-column
        label="学生电话"
        align="center"
        prop="userPhone"
      />
      <el-table-column
        label="订单状态"
        align="center"
        prop="status"
      />
      <el-table-column
        label="订单总金额"
        align="center"
        prop="totalFee"
      />
      <!-- <el-table-column
        label="开始日期"
        align="center"
        prop="startDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="结束日期"
        align="center"
        prop="endDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createdAt"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="订单备注"
        align="center"
        prop="remarks"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="isTutorrequest && scope.row.status == '进行中'"
            @click="complete(scope.row)"
          >完成</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="isTutorrequest && scope.row.status == '已完成'"
            @click="evaluate(scope.row)"
          >评价</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="isTutorrequest && scope.row.status == '已评价'"
            @click="evaluate(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改家教订单对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item
          label="订单编号"
          prop="orderNumber"
        >
          <el-input
            v-model="form.orderNumber"
            placeholder="请输入订单编号"
          />
        </el-form-item>
        <el-form-item
          label="教师ID"
          prop="tutorId"
        >
          <el-input
            v-model="form.tutorId"
            placeholder="请输入教师ID"
          />
        </el-form-item>
        <el-form-item
          label="教师姓名"
          prop="tutorName"
        >
          <el-input
            v-model="form.tutorName"
            placeholder="请输入教师姓名"
          />
        </el-form-item>
        <el-form-item
          label="教师电话"
          prop="tutorPhone"
        >
          <el-input
            v-model="form.tutorPhone"
            placeholder="请输入教师电话"
          />
        </el-form-item>
        <el-form-item
          label="家教申请ID"
          prop="requestId"
        >
          <el-input
            v-model="form.requestId"
            placeholder="请输入家教申请ID"
          />
        </el-form-item>
        <el-form-item
          label="学生ID"
          prop="userId"
        >
          <el-input
            v-model="form.userId"
            placeholder="请输入学生ID"
          />
        </el-form-item>
        <el-form-item
          label="学生姓名"
          prop="userName"
        >
          <el-input
            v-model="form.userName"
            placeholder="请输入学生姓名"
          />
        </el-form-item>
        <el-form-item
          label="学生联系方式"
          prop="userPhone"
        >
          <el-input
            v-model="form.userPhone"
            placeholder="请输入学生联系方式"
          />
        </el-form-item>
        <el-form-item
          label="订单总金额"
          prop="totalFee"
        >
          <el-input
            v-model="form.totalFee"
            placeholder="请输入订单总金额"
          />
        </el-form-item>
        <el-form-item
          label="开始日期"
          prop="startDate"
        >
          <el-date-picker
            clearable
            v-model="form.startDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="结束日期"
          prop="endDate"
        >
          <el-date-picker
            clearable
            v-model="form.endDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="创建时间"
          prop="createdAt"
        >
          <el-date-picker
            clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="订单备注"
          prop="remarks"
        >
          <el-input
            v-model="form.remarks"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="submitForm"
        >确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="评价"
      :visible.sync="openReviews"
      width="65%"
      append-to-body
    >
      <reviews
        v-model="reviewsId"
        @close="openReviews=false; getList();reviewsId=null"
      ></reviews>
    </el-dialog>
  </div>
</template>

<script>
import reviews from './components/reviews'
import {
  listOrders,
  getOrders,
  delOrders,
  addOrders,
  updateOrders,
} from '@/api/system/orders'

export default {
  name: 'Orders',
  components: { reviews },
  data() {
    return {
      isTutorrequest: 1,
      openReviews: false,
      reviewsId: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 家教订单表格数据
      ordersList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNumber: null,
        tutorId: null,
        tutorName: null,
        tutorPhone: null,
        requestId: null,
        userId: null,
        userName: null,
        userPhone: null,
        status: null,
        totalFee: null,
        startDate: null,
        endDate: null,
        createdAt: null,
        remarks: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNumber: [
          { required: true, message: '订单编号不能为空', trigger: 'blur' },
        ],
        tutorId: [
          { required: true, message: '教师ID不能为空', trigger: 'blur' },
        ],
        tutorName: [
          { required: true, message: '教师姓名不能为空', trigger: 'blur' },
        ],
        tutorPhone: [
          { required: true, message: '教师电话不能为空', trigger: 'blur' },
        ],
        requestId: [
          { required: true, message: '家教申请ID不能为空', trigger: 'blur' },
        ],
        userId: [
          { required: true, message: '学生ID不能为空', trigger: 'blur' },
        ],
        userName: [
          { required: true, message: '学生姓名不能为空', trigger: 'blur' },
        ],
        userPhone: [
          { required: true, message: '学生联系方式不能为空', trigger: 'blur' },
        ],
        status: [
          { required: true, message: '订单状态不能为空', trigger: 'change' },
        ],
        totalFee: [
          { required: true, message: '订单总金额不能为空', trigger: 'blur' },
        ],
      },
    }
  },
  created() {
    this.isTutorrequest = this.$store.getters.user.userType == 1 ? false : true
    this.resetQuery()
    this.getList()
  },
  methods: {
    evaluate(row) {
      this.reviewsId = row.id
      this.openReviews = true
    },
    complete(row) {
      this.loading = true
      updateOrders({ ...row, status: '已完成' }).then((v) => {
        this.$modal.msgSuccess('操作成功！')
        this.getList()
      })
    },
    /** 查询家教订单列表 */
    getList() {
      this.loading = true
      listOrders(this.queryParams).then((response) => {
        this.ordersList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNumber: null,
        tutorId:null,
        tutorName: null,
        tutorPhone: null,
        requestId: null,
        userId:null,
        userName: null,
        userPhone: null,
        status: null,
        totalFee: null,
        startDate: null,
        endDate: null,
        createdAt: null,
        remarks: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
      this.queryForm.tutorId = !this.isTutorrequest && this.$store.getters.id
      this.queryForm.userId = this.isTutorrequest && this.$store.getters.id
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加家教订单'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getOrders(id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改家教订单'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateOrders(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {

            addOrders(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除家教订单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delOrders(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'system/orders/export',
        {
          ...this.queryParams,
        },
        `orders_${new Date().getTime()}.xlsx`
      )
    },
  },
}
</script>
